<?php

$api_service_path = __DIR__ . "/../../../../api/";
ob_start();
require_once($api_service_path . "public/index.php");
ob_end_clean();

DB::connection()->disableQueryLog();

$ids=array(1000006617);//单独 发放经验的客户

$user=Users::where("type",1)->whereIn("id",$ids)->select("id");
$user->chunk(1000, function($user) { 
	$id=0;   	
	foreach ($user as $_u) {
		$temp_experience=0;
		$order_amount=Orders::leftJoin("order_pay_record","orders.order_id","=","order_pay_record.order_id")->whereIn('orders.status',array(1018,1017,1217,1317,1417))->where('orders.pay_time',">","2016-06-01 00:00:00")->where('orders.pay_time',"<","2018-07-01 23:59:59")->where("orders.pay_user_id",$_u->id)->where("order_pay_record.status",1)->whereIn("order_pay_record.pay_type",OrderPay::$ei_pay_type)->sum("order_pay_record.amount");
		$service_amount=ServiceCardOrder::leftJoin("service_card_order_pay_record","service_card_order.order_id","=","service_card_order_pay_record.order_id")->whereIn('service_card_order.status',array(1018,1017,1217,1317,1417))->where('service_card_order.pay_time',">","2016-06-01 00:00:00")->where('service_card_order.pay_time',"<","2018-07-01 23:59:59")->where("service_card_order.user_id",$_u->id)->where("service_card_order_pay_record.status",1)->whereIn("service_card_order_pay_record.pay_type",ServiceCardOrderPayRecord::$ei_pay_type)->sum("service_card_order_pay_record.amount");
		$car_wash_amount=CarWashOrder::leftJoin("car_wash_order_pay_record","car_wash_order.order_id","=","car_wash_order_pay_record.order_id")->whereIn('car_wash_order.status',array(1018,1017,1217,1317,1417))->where('car_wash_order.pay_time',">","2016-06-01 00:00:00")->where('car_wash_order.pay_time',"<","2018-07-01 23:59:59")->where("car_wash_order.user_id",$_u->id)->where("car_wash_order_pay_record.status",1)->whereIn("car_wash_order_pay_record.pay_type",CarWashOrderPayRecord::$ei_pay_type)->sum("car_wash_order_pay_record.amount");
		$return_order_amont=ReturnOrder::leftJoin("orders","orders.order_id","=","return_order.order_id")
			->leftJoin("return_order_record","return_order.id","=","return_order_record.return_order_id")
            ->where('return_order.implement_time',"<","2018-07-01 23:59:59")->where("return_order.status",1)->where("orders.pay_user_id",$_u->id)->whereIn("return_order_record.pay_type",OrderPay::$ei_pay_type)->sum("return_order_record.amount");
        $return_wash_amount=ReturnCarwashOrder::leftJoin("car_wash_order","car_wash_order.order_id","=","return_carwash_order.order_id")
        	->leftJoin("return_washorder_record","return_carwash_order.id","=","return_washorder_record.return_washorder_id")->where('return_carwash_order.implement_time',"<","2018-07-01 23:59:59")->where('return_carwash_order.status',1)->where("car_wash_order.user_id",$_u->id)->whereIn("return_washorder_record.pay_type",CarWashOrderPayRecord::$ei_pay_type)->sum("return_washorder_record.amount");
        $return_service_amount=ReturnServiceCarOrder::leftJoin("service_card_order","service_card_order.order_id","=","return_service_card_order.order_id")->leftJoin("return_service_card_record","return_service_card_order.id","=","return_service_card_record.return_order_id")->where('return_service_card_order.implement_time',"<","2018-07-01 23:59:59")->where('return_service_card_order.status',1)->where("service_card_order.user_id",$_u->id)->whereIn("return_service_card_record.pay_type",ServiceCardOrderPayRecord::$ei_pay_type)->sum("return_service_card_record.amount");
        
        $temp_experience=floor(($order_amount+$service_amount+$car_wash_amount-$return_order_amont-$return_wash_amount-$return_service_amount)/100);
        if($temp_experience>0){
        	UserExperienceFlow::record($_u->id,0,"",1,1,$temp_experience,'期初预制经验');
            Users::checkMember($_u->id);
        }
        $id=$_u->id;
	}           
	echo '1000条完毕';   
	Log::info('1000条完毕.ID:'.$id);  	
}); 

// select * from coupons where coupon_base_info_id in(162,163,164,165,166,167,168,169,170,171,172,173,174,175,176);
// update coupons set `status`=1 , received_user_id=0 , get_time='0000-00-00 00:00:00',end_date=null where coupon_base_info_id in(141,140,143);



